package com.zyz.sunliky.quartz;


import jakarta.annotation.PostConstruct;
import org.quartz.*;
import org.springframework.stereotype.Component;



/**
 * @Author zyz
 * @Description 启动定时任务
 */
@Component
public abstract class ScheduledJob implements Job{

    @PostConstruct
    public void init(){
        TaskManager.JOB_LIST.add(this);
    }

    /**
     * cron表达式在线生成 http://cron.qqe2.com/
     *
     * @return
     */
    public abstract String getCron();

    /**
     *
     * @param scheduler
     * @throws SchedulerException
     */
    public void startJob(Scheduler scheduler) throws SchedulerException{
        JobDetail jobDetail = JobBuilder.newJob(this.getClass())
                .withIdentity(this.getClass().getName() + "-job", "group1").build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(getCron());
        CronTrigger cronTrigger = getCronTrigger(scheduleBuilder);
        scheduler.scheduleJob(jobDetail,cronTrigger);
        if (!scheduler.isShutdown()) {
            scheduler.start();
            System.out.println("1");
        }
    }

    /**
     * 若要自定义触发器,请在子类重写此方法
     * @return
     */
    protected CronTrigger getCronTrigger(CronScheduleBuilder scheduleBuilder){
        return TriggerBuilder.newTrigger().withIdentity(this.getClass().getName() + "-trigger", "group1")
                .withSchedule(scheduleBuilder).build();
    }

}
